<!-- 投诉管理 -->
<template>
  <el-form ref="ruleFormRef" :model="ruleForm" inline class="ruleForm">
    <el-form-item label="用户名：" prop="userName">
      <el-input
          v-model="ruleForm.queryRo.userName"
          style="width: 150px"
          clearable
          placeholder="请输用户名"
      />
    </el-form-item>

    <el-form-item label="联系方式：" prop="userPhone">
      <el-input
          v-model="ruleForm.queryRo.userPhone"
          style="width: 150px"
          clearable
          placeholder="请输联系方式"
      />
    </el-form-item>
    <el-form-item label="订单编号：" prop="orderNo">
      <el-input
          v-model="ruleForm.queryRo.orderNo"
          style="width: 150px"
          clearable
          placeholder="请输订单编号"
      />
    </el-form-item>
    <el-form-item label="状态：" prop="complaintStatus">
      <el-select v-model="ruleForm.queryRo.complaintStatus" clearable style="width: 150px">
        <el-option
            v-for="item in complaintStatusList"
            :key="item.value"
            :label="item.name"
            :value="item.value"
        />
      </el-select>
    </el-form-item>

    <el-form-item label="投诉时间：" >
      <el-date-picker
          v-model="complaintTime"
          type="datetimerange"
          :picker-options="pickerOptions"
          range-separator="至"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          value-format="YYYY-MM-DD HH:mm:ss"
      />
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="search">查 询</el-button>
      <el-button type="success" @click="addComplaint()">新增</el-button>
<!--      <el-button type="info" @click="resetComplaint()">重置</el-button>-->
    </el-form-item>
  </el-form>

  <el-table :data="tableList" style="width: 100%" v-loading="loading">
<!--    <el-table-column prop="id" label="序号" align="center" width="100px" />-->
    <el-table-column prop="userName" label="用户名" align="center" width="100px" />
    <el-table-column prop="userPhone" label="联系方式" align="center" width="200px" />
    <el-table-column prop="complaintContent" label="投诉内容" align="center" width="300px" />
    <el-table-column prop="complaintObject" label="投诉对象" align="center" width="100px">
      <template #default="scope">
        <span >{{ complaintObjectNum(scope.row.complaintObject) }}</span>
      </template>
    </el-table-column>
    <el-table-column prop="orderNo" label="订单号" align="center" width="200px"/>
    <el-table-column prop="complaintStatus" label="状态" align="center" width="100px">
      <template #default="scope">
        <span >{{ complaintStatusNum(scope.row.complaintStatus) }}</span>
      </template>
    </el-table-column>
    <el-table-column prop="complaintTime" label="投诉时间" align="center" width="200px"/>
    <el-table-column label="操作" align="center" width="250px">
      <template #default="scope">
        <el-button link @click="openDialog(scope.row)" type="primary">查看详情</el-button>
        <el-button link type="primary" size="small" @click="editComplaint(scope.row)">编辑</el-button>
        <el-button link type="danger" size="small" @click="delComplaint(scope.row)">删除</el-button>
        <el-button link type="warning" size="small" @click="dealComplaint(scope.row)">处理</el-button>
      </template>
    </el-table-column>
  </el-table>

  <footer class="pagination">
    <el-pagination
      v-model:currentPage="ruleForm.current"
      v-model:page-size="ruleForm.pageSize"
      :page-sizes="[10, 20, 30, 50]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      small
    />
  </footer>

  <el-drawer
      v-model="drawer"
      :title="tableForm.data.id ? '编辑投诉资料': '新增投诉资料'"
      :direction="direction">
    <el-form
        ref="ruleFormRef"
        :model="tableForm.data"
        :rules="rules"
        label-width="120px"
        class="demo-ruleForm"
        :size="formSize"
        status-icon
    >
      <el-form-item label="用户名" prop="userName">
        <el-input v-model="tableForm.data.userName" placeholder="请填写用户名" />
      </el-form-item>
      <el-form-item label="联系方式" prop="userPhone">
        <el-input v-model="tableForm.data.userPhone" placeholder="请填联系方式" />

      </el-form-item>
      <el-form-item label="投诉内容" prop="complaintContent">
        <el-input
            maxlength="250"
            type="textarea"
            :rows="2"
            v-model="tableForm.data.complaintContent"
            placeholder="请填投诉内容"
            show-word-limit>
        </el-input>
<!--        <el-input v-model="tableForm.data.complaintContent" placeholder="请填投诉内容" />-->
      </el-form-item>
      <el-form-item label="投诉对象" prop="complaintContent">
        <el-select v-model="tableForm.data.complaintObject" clearable style="width: 150px" placeholder="请选择投诉对象">
          <el-option
              v-for="item in complaintObjectList"
              :key="item.value"
              :label="item.name"
              :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="订单号" prop="orderNo">
        <el-input v-model="tableForm.data.orderNo" placeholder="请填订单号" />
      </el-form-item>
<!--      <el-form-item label="状态" prop="complaintStatus">-->
<!--        <el-select v-model="tableForm.data.complaintStatus" clearable style="width: 150px" placeholder="请选择状态">-->
<!--          <el-option-->
<!--              v-for="item in complaintStatusList"-->
<!--              :key="item.value"-->
<!--              :label="item.name"-->
<!--              :value="item.value"-->
<!--          />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="投诉时间" prop="complaintTime">-->
<!--        <el-input v-model="tableForm.data.complaintTime" placeholder="请填站点序号" />-->
<!--      </el-form-item>-->
    </el-form>
    <template #footer>
      <div style="flex: auto">
        <!--        <el-button @click="cancelForm">取消</el-button>-->
        <el-button type="primary"  @click="submitForm(ruleFormRef)">确定</el-button>
      </div>
    </template>
  </el-drawer>
  <complaintDetailDialog
      v-model:show="showDialog"
      :detail="detail"
      @openDialog="openDialog"
  ></complaintDetailDialog>
</template>

<script lang="ts" setup>
import { ref, reactive, onMounted } from 'vue'
import complaintDetailDialog from './components/complaintDetailDialog.vue'
import api from '@/api/apis/complaintApi'
import {ElMessage, ElMessageBox, FormInstance, FormRules} from "element-plus";

const drawer = ref(false)
const ruleFormRef = ref<FormInstance>()

//编辑
const editComplaint = (row)=>{
  tableForm.data= {...row}
  // status.value = ruleForm.data.status == 1 ? true : false
  // ruleForm.data.status = 1
  drawer.value = true
  console.log('ruleForm',tableForm.data)
}

const tableForm = reactive({
  data:{
    id:"",
    userName:"",
    userPhone: "",
    complaintContent: "",
    complaintObject: "",
    orderNo:"",
    complaintStatus:"",
    complaintTime:"",
  },
})

const ruleForm = reactive({
  queryRo:{
    userName:"",
    userPhone:"",
    orderNo:"",
    complaintTime:"",
    complaintStatus:"",
    // status:undefined
  },

  current: 1,
  pageSize: 50,
  searchCount: true
})
const time=ref<any>([])
const complaintTime=ref<any>('')


//总数
const total = ref(0)
//搜索
const search = () => {
  ruleForm.current = 1
  getTableList()
}

//新增
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      let res, message
      // ruleForm.data.sex = sex.value
      console.log('data',tableForm.data)
      if(tableForm.data.id){
        res = await api.updateComplaint(tableForm.data)
        message = '编辑成功！'
      }else {
        res = await api.addComplaint(tableForm.data)
        message = '新增成功！'
      }
      if(res.data.code == 200){
        ElMessage({
          message: message,
          type: 'success',
        })
        getTableList()
        drawer.value = false
      }
    } else {
      console.log('error submit!', fields)
    }
  })
}


// 新增
const addComplaint = async (formEl: FormInstance | undefined) => {
  drawer.value = true
  let  obj = {
    userName:"",
    userPhone: "",
    complaintContent: "",
    complaintObject: "",
    orderNo:"",
    complaintStatus:"",
    complaintTime:"",
  }
  tableForm.data = obj
}

// 必填规则
const rules = reactive<FormRules>({
  userName: [
    { required: true, message: '请填用户名', trigger: 'blur' },
  ],
  userPhone: [
    { required: true, message: '请填联系方式', trigger: 'blur' },
  ],
  complaintContent: [
    { required: true, message: '请填投诉内容', trigger: 'blur' },
  ],
  complaintObject: [
    { required: true, message: '请填投诉对象', trigger: 'blur' },
  ],
  orderNo: [
    { required: true, message: '请填订单号', trigger: 'blur' },
  ],
  // complaintStatus: [
  //   { required: true, message: '请填结束充电电量', trigger: 'blur' },
  // ],
  // complaintTime: [
  //   { required: true, message: '请填结束充电电量', trigger: 'blur' },
  // ],
})

const delComplaint = (row)=>{
  ElMessageBox.confirm(
      '您确定要删除该投诉吗?',
      '注意',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      }
  )
      .then(async () => {
        let res = await api.delComplaintById(row.id)
        console.log(res)
        if(res.data.code == 200){
          ElMessage({
            type: 'success',
            message: '删除成功',
          })
          getTableList()
        }

      })
      .catch(() => {
        ElMessage({
          type: 'info',
          message: '取消删除',
        })
      })

}

const dealComplaint = (row)=>{
  ElMessageBox.confirm(
      '您确定要处理改投诉吗?',
      '注意',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      }
  )
      .then(async () => {
        let res = await api.dealComplaintById(row.id)
        console.log(res)
        if(res.data.code == 200){
          ElMessage({
            type: 'success',
            message: '处理成功',
          })
          getTableList()
        }

      })
      .catch(() => {
        ElMessage({
          type: 'info',
          message: '取消处理',
        })
      })

}

const complaintObjectNum = (value: string) => {
  let obj: any = {
    1: '平台',
    2: '司机',
  }
  return obj[value]
}

const complaintStatusNum = (value: string) => {
  let obj: any = {
    1: '进行中',
    2: '已解决',
  }
  return obj[value]
}
const complaintObjectList=[
  {name:"平台",value:1},
  {name:"司机",value:2},
]

const complaintStatusList=[
  {name:"进行中",value:1},
  {name:"已解决",value:2},
]


//表格数据
const tableList = ref([])
//加载
const loading = ref(false)
onMounted(()=>{
  getTableList()
})
//获取表格数据
const getTableList = async () => {
  loading.value = true
  try {
    let obj = { ...ruleForm }
    // obj.queryRo.chargeBeginTime =time.value?time.value[0]:undefined
    // obj.queryRo.chargeEndTime =time.value?time.value[1]:undefined
    obj.queryRo.complaintTime =complaintTime.value?complaintTime.value:undefined
    let {data} = await api.complaintPage(obj)
    tableList.value= data.data.rows
    total.value= Number(data.data.total)

  } catch (error) {
    loading.value = false
  }
  loading.value = false
}
// //改变每页条数
const handleSizeChange = (val: number) => {
  ruleForm.current = 1
  ruleForm.pageSize = val
  getTableList()
}
//改变当前页数
const handleCurrentChange = (val: number) => {
  ruleForm.current = val
  getTableList()
}

//弹框开关
const showDialog = ref(false)
//详情id
const detail = ref()
//打开详情弹框
const openDialog = (val: string) => {
  detail.value = val
  console.log('值---',val)
  showDialog.value = true
}

</script>

<style scoped lang="scss">
footer {
  display: flex;
  justify-content: center;
  margin-top: 15px;
}
</style>
